package com.h4313.ghome.ui;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.h4313.ghome.R;
import com.h4313.ghome.io.CreateModuleTask;
import com.h4313.ghome.io.CreateModuleTask.CreateModuleCallback;
import com.h4313.ghome.ui.fragments.CreateMorningConfFragment;
import com.h4313.ghome.ui.fragments.CreateSecurityFragment;
import com.h4313.ghome.ui.fragments.CreateTemperatureFragment;
import com.h4313.ghome.ui.fragments.ModulesListFragment;
import com.h4313.ghome.ui.fragments.ModulesListFragment.OnModuleSelected;

public class NewModuleActivity extends Activity implements OnModuleSelected, NewModuleCreation, CreateModuleCallback {

	public static final String RESULT_MODULE_NAME = "modulename";
	public static final String RESULT_MODULE_ID = "id";
	

	private String [] mModules;

	private ModulesListFragment modulesList;
	private HashMap<String, Fragment> mFragMap = new HashMap<String, Fragment>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mModules = getResources().getStringArray(R.array.type_module);
		
		setContentView(R.layout.activity_newmodule);


		ArrayAdapter<String> modulesAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mModules);
		modulesList = (ModulesListFragment) getFragmentManager()
				.findFragmentById(R.id.frag_modules_list);

		modulesList.setListAdapter(modulesAdapter);
	}

	@Override
	public void showModule(String module) {
		Fragment moduleFragment = mFragMap.get(module);
		if (moduleFragment == null) {
			moduleFragment = moduleFragmentFactory(module);
			mFragMap.put(module, moduleFragment);
		}
		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();
		transaction.replace(R.id.container_modules, moduleFragment);
		transaction.commit();
	}

	
	private Fragment moduleFragmentFactory(String name) {
		if(name.equals(mModules[0])) {
			return CreateTemperatureFragment.newInstance(mModules[0]);
		} else if (name.equals(mModules[1])) {
			return CreateMorningConfFragment.newInstance(mModules[1]);
		} else if (name.equals(mModules[2])) {
			return CreateSecurityFragment.newInstance(mModules[2]);
		}
		return null;

	}

	@Override
	public void createModule(String moduleName, Map<String, String> params) {
		CreateModuleTask task = new CreateModuleTask(moduleName, this);
		task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
	}

	@Override
	public void onModuleCreated(String moduleName, int id) {
		Bundle bundle = new Bundle();
		bundle.putString(RESULT_MODULE_NAME, moduleName);
		bundle.putInt(RESULT_MODULE_ID, id);
		
		Intent intent = new Intent();
		intent.putExtras(bundle);
		
		setResult(RESULT_OK, intent);
		finish();
	}
}
